<template>
	<view>
		<view class="comment-item" @click="goReplay">
			<view class="avatar">
				<u-avatar :src="giveAvatar(item)" size="26"></u-avatar>
			</view>

			<view class="wrap">
				<view class="username">
					{{ giveName(item) }}
					<!-- 阻止冒泡 -->
					<text v-if="!closeBtn" class="iconfont icon-a-123-cuowu" @click.stop="closeComment"></text>
				</view>
				<view class="comment-content">{{ item.comment_content }}</view>
				<view class="info">
					<view class="reply-btn" v-if="!childStatus">{{item.totalStudents}}回复</view>
					<view>
						<uni-dateformat :date="item.comment_date" :threshold="[60000, 3600000 * 24 * 30]"></uni-dateformat>
					</view>
					<view>{{ item.cityStr }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { giveName, giveAvatar } from '../../utils/tools.js';

const db = uniCloud.database();
// 引入云对象
const utilsObj = uniCloud.importObject('utilsObj', {
	// 移除自带loadding
	customUI: true
});
export default {
	name: 'comment-item',
	props: {
		item: {
			type: Object,
			default: () => {
				return {};
			}
		},
		childStatus:{
			type:Boolean,
			default:false
		},
		closeBtn:{
			type:Boolean,
			default:false
		}
	},
	data() {
		return {};
	},
	methods: {
		giveName,
		giveAvatar,
		// 确认删除
		closeComment() {
			let uid = uniCloud.getCurrentUserInfo().uid;
			let currentId = this.item.user_id[0]._id;
			if (uid === currentId || this.uniIDHasRole('admin')) {
				uni.showModal({
					title: '是否确认删除？',
					success: (res) => {
						if (res.confirm) {
							this.removeComment();
						}
					}
				});
				return;
			}
			uni.showToast({
				icon: 'error',
				title: '权限不足'
			});
		},
		// 删除
		async removeComment() {
			await db.collection('quanzi-comment').doc(this.item._id).remove();
			uni.showToast({
				title: '删除成功'
			});
			// 子传父实现无感刷新
			this.$emit('removeFun', { id: this.item._id });

			//减少评论数量
			utilsObj.operation('quanzi_artcle', 'comment_count', this.item.article_id, -1);
		},
		// 跳转到二级评论
		goReplay() {
			if(this.childStatus)return
			uni.setStorageSync('replyItem', JSON.stringify(this.item));
			uni.navigateTo({
				url: '/pages/replay/replay'
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.comment-item {
	display: flex;
	.wrap {
		flex: 1;
		padding-left: 20rpx;
		padding-bottom: 20rpx;

		.username {
			font-size: 26rpx;
			color: #666;
			padding: 10rpx 0;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.iconfont {
				font-size: 40rpx;
				padding: 10rpx;
				color: #999;
			}
		}

		.comment-content {
			font-size: 34rpx;
			color: #111;
			line-height: 1.8em;
		}

		.info {
			font-size: 26rpx;
			color: #666;
			display: flex;
			padding: 10rpx 0;
			align-items: center;

			view {
				margin-right: 25rpx;
			}

			.reply-btn {
				padding: 6rpx 18rpx;
				background: #e4e4e4;
				border-radius: 30rpx;
			}
		}
	}
}
</style>
